class Solution(object):
    def highestRankedKItems(self, grid, pricing, start, k):
        ans = []
        m = len(grid)
        n = len(grid[0])
        used = [[0 for _ in range(n)] for _ in range(m)]
        used[start[0]][start[1]] = 1
        now = [start]
        if pricing[0] <= grid[start[0]][start[1]] <= pricing[1]:
            ans.append(start)
        while now and len(ans) < k:
            next_st = []
            ans_st = []
            for x, y in now:
                for (ux, uy) in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
                    if 0 <= ux < m and 0 <= uy < n and grid[ux][uy] > 0 and not used[ux][uy]:
                        used[ux][uy] = 1
                        next_st.append([ux, uy])
                        if pricing[0] <= grid[ux][uy] <= pricing[1]:
                            ans_st.append([grid[ux][uy], ux, uy])
            now = next_st
            ans_st.sort()
            ans_st = [[x, y] for _, x, y in ans_st]
            ans += ans_st
        return ans[:k]
